package com.ktjy.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ktjy.dto.ClueDTO;
import com.ktjy.dto.ClueTrackRecordDTO;
import com.ktjy.pojo.TbAssignRecord;
import com.ktjy.pojo.TbClue;
import com.ktjy.service.ITbBusinessService;
import com.ktjy.service.ITbClueService;
import com.ktjy.utils.AjaxResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 线索 前端控制器
 * </p>
 *
 * @author jjx
 * @since 2025-08-19
 */
@RestController
@RequestMapping("/api/tb/clue")
@Slf4j
@Tag(name="线索管理")
public class TbClueController {
    @Autowired
    ITbClueService clueService;
    @Autowired
    ITbBusinessService businessService;

    @Operation(summary="线索列表查询")
    @GetMapping("/list")
    public AjaxResult list(ClueDTO clueDTO,
                           @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                           @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        clueDTO.setStatuses(new String[]{"1","2"});
        IPage<TbClue> page = clueService.findPage(clueDTO, pageNo, pageSize);
        return AjaxResult.success(page);
    }

    @Operation(summary="查询线索池")
    @GetMapping("/pool")
    public AjaxResult pool(ClueDTO clueDTO,
                           @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                           @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize) {
        clueDTO.setStatuses(new String[]{"3","4"});
        IPage<TbClue> page = clueService.findPage(clueDTO, pageNo, pageSize);
        return AjaxResult.success(page);
    }

    @Operation(summary="新增线索")
    @PostMapping("/save")
    public AjaxResult save(@RequestBody TbClue tbClue){
        try {
            clueService.saveClue(tbClue);
            return AjaxResult.success("保存成功");
        } catch (Exception e) {
            log.error("TbClueController中save方法执行报错的。",e);
        }
        return AjaxResult.error("保存失败");
    }

    @Operation(summary="修改线索")
    @PostMapping("/update")
    public AjaxResult update(@RequestBody TbClue tbClue){
        try {
            boolean ok=clueService.updateById(tbClue);
            if(ok){
                return AjaxResult.success("保存成功");
            }
        } catch (Exception e) {
            log.error("TbClueController中update方法执行报错的。",e);
        }
        return AjaxResult.error("保存失败");
    }

    @Operation(summary="查询线索详情")
    @GetMapping("/view/{id}")
    public AjaxResult view(@PathVariable("id") Long id){
        //查询用户详情
        try {
            TbClue tbClue=clueService.getClueById(id);
            if(tbClue!=null){
                return AjaxResult.success(tbClue);
            }
        } catch (Exception e) {
            log.error("TbClueController中view方法执行报错的。",e);
        }
        return AjaxResult.error();
    }

    @Operation(summary="删除线索")
    @DeleteMapping("/del/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            boolean ok= clueService.removeById(id);
            if(ok){//删除成功
                return AjaxResult.success("删除成功");
            }
        } catch (Exception e) {
            log.error("TbClueController中del方法执行报错的。",e);
        }
        return AjaxResult.error("删除失败");
    }

    @Operation(summary="线索分配")
    @PutMapping("/assignClue")
    public AjaxResult assignClue(@RequestBody TbAssignRecord assignRecord) {
        try {
            clueService.assignClue(assignRecord.getAssignId(), assignRecord.getUserId());
            return AjaxResult.success("分配成功");
        } catch (Exception e) {
            log.error("TbClueController中assignClue方法执行报错的。",e);
        }
        return AjaxResult.error("分配失败");
    }

    @Operation(summary="伪线索")
    @PutMapping("/falseClue")
    public AjaxResult falseClue(@RequestBody ClueTrackRecordDTO clueTrackRecordDTO) {
        try {
            clueService.falseClue(clueTrackRecordDTO.getClueId(),
                    clueTrackRecordDTO.getFalseReason(), clueTrackRecordDTO.getRecord());
            return AjaxResult.success("设置伪线索成功");
        } catch (Exception e) {
            log.error("TbClueController中falseClue方法执行报错的。",e);
        }
        return AjaxResult.error("设置伪线索失败");
    }

    @Operation(summary="捞取线索")
    @PutMapping("/gainClue/{id}")
    public AjaxResult gainClue(@PathVariable("id") Long id) {
        try {
            clueService.gainClue(id);
            return AjaxResult.success("捞取线索成功");
        } catch (Exception e) {
            log.error("TbClueController中gainClue方法执行报错的。",e);
        }
        return AjaxResult.error("捞取线索失败");
    }

    @Operation(summary="线索转商机")
    @PutMapping("/changeBusiness/{id}")
    public AjaxResult changeBusiness(@PathVariable Long id) {
        try {
            businessService.changeBusiness(id);
            return AjaxResult.success("线索转商机成功");
        } catch (Exception e) {
            log.error("TbClueController中changeBusiness方法执行报错的。",e);
        }
        return AjaxResult.error("线索转商机失败");
    }
}
